﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using InstrumentCheck.BLL;
using InstrumentCheck.Module;

namespace InstrumentCheck.WebUI
{
    public class BasePage : System.Web.UI.Page
    {
        public BasePage()
        { }

        protected override void OnLoad(EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!PublicFunction.CheckUser(this.Page))
                {
                    this.AlertMessage("對不起，您沒有權限登錄該系統！","close");
                    return;
                }
            }

            if (Session["UserInfo"] == null)
            {
                this.AlertMessage("對不起，您還沒有登錄或在系統閒置時間過久，請重新登入!", "close");
                return;
            }

            base.OnLoad(e);
        }

        protected void AlertMessage(String message)
        {
            AlertMessage(message, String.Empty);
        }

        protected void AlertMessage(String message, String url)
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script language=\"javascript\">alert(\"" + message + "\");</script>");
            if (url.Length > 0)
            {
                if (url.ToUpper() != "CLOSE")
                {
                    String script = String.Format("<script language=\"javascript\">window.location.href=\"{0}\";</script>", url);
                    this.ClientScript.RegisterStartupScript(this.GetType(), "GoURL", script);
                }
                else
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "Close", "<script language=\"javascript\">window.close();</script>");
                }
            }
        }

        protected string AttachURL()
        {
            string str1 = Request["URL"];
            string str2 = Request["HTTP_HOST"];
            string strAll = "http://" + str2 + str1.Substring(0, str1.LastIndexOf("/")) + "/Attachment/";
            return strAll;
        }

        //protected string AttachmentPath()
        //{
        //    return System.Configuration.ConfigurationManager.AppSettings["AttachPath"].ToString();
        //}
    }
}
